<template>
  <div class="table-box">
    <ProTable ref="pTable" :columns="columns" :init-param="initParam" :request-api="getCompanyBillingVoListApi">
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="handleOpenDialog(OptEnum.CREATE)"> 新增开票申请 </el-button>
      </template>
      <template #status="scope">
        <el-tag :type="scope.row.status === sysEnum.EIGHT ? '' : 'danger'" style="margin-left: 5px">
          {{ filterEnum(scope.row.status, sysStatusType) }}
        </el-tag>
      </template>
      <template #operation="scope">
        <el-button type="primary" :icon="EditPen" size="small" text @click="handleOpenDialog(OptEnum.EDIT, scope.row)">
          编辑
        </el-button>
        <el-button
          type="danger"
          :icon="Delete"
          size="small"
          text
          :disabled="scope.row.status == sysEnum.EIGHT"
          @click="handleDeleteCompany(scope.row)"
        >
          删除
        </el-button>
      </template>
    </ProTable>
    <CompanyBillingDialog ref="dialogRef" @refresh-list="handleRefreshList" />
  </div>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import ProTable from "@/components/ProTable/index.vue";
import CompanyBillingDialog from "./components/CompanyBillingDialog.vue";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getCompanyBillingVoListApi, deleteCompanyBillingApi } from "@/api/modules/cpy/coin";
import { ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { OptEnum, sysEnum } from "@/biz/eenum/appEnum";
import { filterEnum } from "@/utils";
import { sysStatusType } from "@/biz/datas/sysData";
import { CompanyBillingVo } from "@/biz/interface/CompanyType";

const pTable = ref();
// 固定在列表中的参数
const initParam = reactive({});
// 表格列配置
const columns: Partial<ColumnProps>[] = [
  { type: "selection", width: 80, fixed: "left" },
  { type: "index", label: "#", width: 60 },
  {
    prop: "billingNo",
    label: "开票单号",
    search: { el: "input" }
  },
  {
    prop: "status",
    label: "状态",
    enum: sysStatusType,
    search: { el: "select" },
    width: 100
  },
  {
    prop: "billingAmt",
    label: "开票金额",
    width: 100
  },
  {
    prop: "contactor",
    label: "联系人",
    search: { el: "input" },
    width: 120
  },
  {
    prop: "contactorPhone",
    label: "联系电话",
    search: { el: "input" },
    width: 120
  },
  {
    prop: "applyUserName",
    label: "申请人",
    width: 120
  },
  {
    prop: "applyDate",
    label: "申请时间"
  },
  {
    prop: "aduitUserName",
    label: "审核人",
    width: 120
  },
  {
    prop: "aduitDate",
    label: "审核时间"
  },
  {
    prop: "operation",
    width: 200,
    fixed: "right",
    label: "操作"
  }
];

//刷新数据列表
const handleRefreshList = () => {
  pTable.value.getTableList();
};

// 删除信息
const handleDeleteCompany = async (params: CompanyBillingVo) => {
  await useHandleData(deleteCompanyBillingApi, { id: params.id! }, `删除单号为【${params.billingNo}】的开票信息`);
  pTable.value.getTableList();
};

// 打开 dialog(新增、查看、编辑)
const dialogRef = ref();
const handleOpenDialog = (optType: OptEnum, rowData?: { id: string }) => {
  let params = {
    optType: optType,
    rowData: { ...rowData }
  };
  dialogRef.value.acceptParams(params);
};
</script>
